<template>
  <div>

    <!--头部-->
    <div style="width: 80%;margin-left: 20%;">
      <span>
<!--        <h1>哈喽商城</h1>-->
        <!--        <h1>购物车</h1>-->
      </span>
    </div>

    <!--表头-->
    <div class="title" style="width: 80%;margin-left: 10%;">
      <h3>购物车</h3>
      <el-table
        ref="multipleTable"
        :data="tableData.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        @selection-change="handleSelectionChange"
        style="width: 100%;">
        <!--        多选框-->
        <el-table-column align="center" type="selection" width="55" :selectable="canSelect"></el-table-column>
        <!--        商品图片-->
        <el-table-column label="商品" prop="img" width="110px" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px;" :src="scope.row.img"/>
          </template>
        </el-table-column>
        <!--        商品名字-->
        <el-table-column label="商品名" prop="name" align="center"></el-table-column>
        <!--        上下架状态-->
        <el-table-column label="状态" prop="prize" width="110px" align="center">
          <template slot-scope="scope">
            <el-tag :type="goodsStatus(scope.row.status)">{{scope.row.status==1?"下架":"在售"}}</el-tag>
          </template>
        </el-table-column>
        <!--        商品单价-->
        <el-table-column label="单价" prop="prize" width="110px" align="center">
          <template slot-scope="scope">
            <span>&yen;</span>{{scope.row.prize}}
          </template>
        </el-table-column>
        <!--        商品数量-->
        <el-table-column label="数量" prop="num" width="140px" align="center">
          <template slot-scope="scope">
            <el-input-number size="mini" v-model="scope.row.nums"
                             :disabled="scope.row.status==1?true:false"></el-input-number>
          </template>
        </el-table-column>
        <!--        商品小计-->
        <el-table-column label="小计" prop="allPrize" width="110px" align="center">
          <template slot-scope="scope">
            <span>&yen;</span>{{parseFloat(scope.row.prize*scope.row.nums).toFixed(2)}}
          </template>
        </el-table-column>
        <!--        操作-->
        <el-table-column align="right">
          <template slot="header" slot-scope="scope">
            <el-input v-model="search" size="mini" placeholder="输入关键字搜索"/>
          </template>
          <template slot-scope="scope">
            <el-button size="mini" :disabled="scope.row.status==1?true:false" type="warning"
                       @click="handleEdit(scope.$index, scope.row)">移到收藏
            </el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">移出购物车</el-button>
            <el-button size="mini" :disabled="scope.row.status==1?true:false" type="success" @click="goToclean(scope)">
              结算
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="width: 80%;margin-left: 10%;">

      <!--描述：商品结算开始-->
      <div class="balance">
        <ul class="balance_ul1">
          <el-button type="danger" size="medium" class="button1" plain @click="deleteGoods">删除选中商品</el-button>
          <el-button type="danger" size="medium" class="button1" plain @click="dofilter">清除下架商品</el-button>
          <el-button type="warning" size="medium" class="button1" plain>移到我的收藏</el-button>
          <el-button type="success" size="medium" class="button1" plain @click="goToDOubleClean">结算选中商品</el-button>
          <span class="balance_ul2">
              <span>共<span class="spanText">{{changeNumPri(1)}}</span>件商品</span>
              <span>总价<span class="spanText">&yen;{{changeNumPri(2)}}</span></span>
              <span>
<!--                <router-link :to="{ path:'/makeSureOrder',query:{tableData:this.tableData}}"></router-link>-->
                  <el-button @click="cleanAll">全部结算</el-button>

              </span>
          </span>
        </ul>
      </div>
      <!--描述：商品结算结束-->
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import store from "../../store";
  export default {
    name: "carts",
    data() {
      return {
        tableData: [],
        search: '',

        multipleSelection: [],//选中列表
        chooseList: [],
      }
    },
    created() {

      const data1 = JSON.parse(localStorage.getItem('carts'));
      if (data1!=null){
        this.tableData=data1
      }else{
        this.gitInit()
      }
      //console.log(this.$route.query.tableDataOne.nums!={})
      if (this.$route.query.tableDataOne.nums!=null){
        //this.tableData.push(this.$route.query.tableDataOne)
      //store.commit('addCarts',this.$route.query.tableDataOne)
        this.tableData.push(this.$route.query.tableDataOne)
      }
      //后端取购物车数据
     // this.tableData=axios.get("http://localhost:8081/getcarts")

      localStorage.setItem('carts',JSON.stringify(this.tableData))


    },
    methods: {
      deleteGoods(){//删除选中商品
        console.log(this.multipleSelection)
        this.tableData.pop()

      },
      gitInit(){
        this.tableData=store.getters.getCarts
        console.log(this.tableData)
      },
      //todo  更新数据库
      updateData(){
        axios.put('',this.tableData)
      },
      //全部结算
      cleanAll(){
        if (this.tableData.some(item=>item.status==1)){
          this.$notify.error({
            title: '错误',
            message: '购物车包含下架商品'
          });
        }else{
          this.$router.push({ path:'/makeSureOrder',query:{tableData:this.tableData}})
        }

      },
      //清除下架商品
      dofilter(){
        this.tableData=this.tableData.filter(
          item=> item.status==2

        )
      },
      //单点一个结算
      goToclean(val) {
        // console.log(val.row)
        const a=[val.row]
        this.$router.push(
          {
            path: '/makeSureOrder',
            query: {
              tableData: a
            }
          }
        )
      },
      //多选，选下面的结算
      goToDOubleClean(){
        this.$router.push(
          {
            path: '/makeSureOrder',
            query: {
              tableData: this.multipleSelection
            }
          }
        )
      },
      canSelect(row) {
        if (row.status == 1)
          return false;
        else return true;
      },
      goodsStatus(status) {
        if (status == "1")
          return "danger";
        else if (status == "2")
          return "";
      },
      changeNumPri(which) {
        const length = this.tableData.length;
        //总价
        //全部数量
        let allPricess = 0.0;
        let allNumss = 0;
        for (let i = 0; i < length; i++) {
          if (this.tableData[i].status != 1) {
            allPricess += this.tableData[i].nums * this.tableData[i].prize;
            allNumss += this.tableData[i].nums;
          }
        }
        if (which == '2') {
          return parseFloat(allPricess).toFixed(2);
        } else {
          return allNumss;
        }
      },
      // 多选操作
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
    }
  }
</script>

<style scoped>
  .title {
    margin-top: 50px;
  }

  .title h3 {
    /*float: left;*/
    font-size: 23px;
    color: #c91623;
  }

  /*商品结算*/
  .balance {
    height: 50px;
    border: 1px solid gray;
    margin-top: 40px;
  }

  .balance li {
    float: left;
    line-height: 5px;
    margin-left: 22px;
  }

  .balance_ul2 {
    float: right;
    margin-right: 10px;
  }

  .balance_ul1 .button1 {
    margin-top: 5px;
  }

  .balance_ul2 .spanText {
    font-size: 25px;
    color: #C91623;
    font-weight: bold;
  }

  .balance_ul2 button {
    width: 100px;
    height: 50px;
    background-color: brown;
    border: 1px solid #c91623;
    font-weight: bold;
    font-size: 20px;
    color: white;
  }

  .balance_ul2 button:hover {
    background-color: #C91623;
  }
</style>
